﻿using D.Medicalcare.ErrorCode;
using D.Medicalcare.Write.API.Application.Commands.DoctorCommand;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Org.BouncyCastle.Asn1.Ocsp;

namespace D.Medicalcare.Write.API.Controllers
{
    /// <summary>
    /// 医生管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DoctorController : ControllerBase
    {

        private readonly IMediator mediator;

        public object ApiEnum { get; private set; }

        public DoctorController(IMediator mediator)
        {
            this.mediator = mediator;
        }



       /// <summary>
       /// 医生管理添加
       /// </summary>
       /// <param name="request"></param>
       /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<int>> AddDoctor(AddDoctorCommand request)
        {

            try
            {
                return await mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }


        }


        /// <summary>
        /// 医生管理修改
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<int>> UpdateDoctor(UpdateDoctorCommand request)
        {
            try
            {
                return await mediator.Send(request);
            }
            catch (Exception)
            {

                throw;
            }

        }

        /// <summary>
        /// 医生管理删除
        /// </summary>
        /// <param name="DoctorId"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<int>> DelDoctor(int DoctorId)
        {
            try
            {
                DelDoctorCommand delDoctorCommand = new DelDoctorCommand();
                delDoctorCommand.DoctorId = DoctorId;
                return await mediator.Send(delDoctorCommand);

            }
            catch (Exception)
            {

                throw;
            }
        }



        /// <summary>
        /// 图片上传
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> DoctorImages(IFormFile file)
        {
            try
            {
                APIResult<string> result = new APIResult<string>() { Code = APIEnum.Success };
                var res = "wwwroot/Images/" + file.FileName;
                using (FileStream fs = new FileStream(res, FileMode.OpenOrCreate))
                {
                    file.CopyTo(fs);
                }
                return Ok("https://localhost:7027/Images/" + file.FileName);
            }
            catch (Exception)
            {
               

                throw;
            }
        }


    }
}
